home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / imc9101.zip / TRAPDOOR.C < prev   
C/C++ Source or Header  |  1991-01-18  |  4KB  |  134 lines

  1. /****************************************************************
  2. *    TRAPDOOR.C - A program to perform trap-door encryption.        *
  3. *    To compile: cl trapdoor.c                                    *
  4. *    Anthony D. Ennis, 4 Aug 1990                                *
  5. ****************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #define MAX_WHEELS 10            /* Maximum number of keys        */
  12. #define MAX_KEY_LEN 32            /* Maximum length of each key    */
  13.  
  14. /****************************************************************
  15. * the code "wheel" definitions                                    *
  16. ****************************************************************/
  17. struct wheel_def
  18.        {
  19.     char *pos;                        /* Next char to XOR against    */
  20.     char value[MAX_KEY_LEN];        /* String of key characters    */
  21.     } wheel[MAX_WHEELS];            /* Array of keys            */
  22. int n_wheels;                        /* Number of "wheels" to use*/
  23.  
  24. /****************************************************************
  25. * tdInit - initialize the trapdoor "wheels"                        *
  26. * NOTE:    the array "wheel" is full                                *
  27. ****************************************************************/
  28. void tdInit(void)
  29.     {
  30.     int i;
  31.  
  32.     for (i=0; i < n_wheels; i++)
  33.         wheel[i].pos = &wheel[i].value[0];
  34.     }
  35.  
  36. /****************************************************************
  37. * tdEncrypt - encrypt the current character                        *
  38. * INP:    c - the character to encrypt                            *
  39. * OUT:    the encrypted equivalent of the character                *
  40. ****************************************************************/
  41. int tdEncrypt(int c)
  42.     {
  43.     int i;
  44.  
  45.     /* XOR the character against current char in each wheel    */
  46.     for (i=0; i<n_wheels; i++)
  47.         {
  48.         /* XOR character against the code wheel */
  49.         c ^= *wheel[i].pos;
  50.  
  51.         /* Point to the next letter on code wheel */
  52.         if ( *++wheel[i].pos == '\0')
  53.             wheel[i].pos = wheel[i].value;
  54.         }
  55.     return c;
  56.     }
  57.  
  58. /****************************************************************
  59. * GetFile - prompt user for file name, open & return handle        *
  60. *                                                                *
  61. * INP:    prompt - prompt string                                    *
  62. *        mode - mode to use when opening file                    *
  63. * OUT:    NULL if error, otherwise an open file handle            *
  64. ****************************************************************/
  65. FILE *GetFile(char *prompt, char *mode)
  66.     {
  67.     FILE *retval;
  68.     char filename[100];
  69.  
  70.     fputs(prompt,stdout);
  71.     scanf("%s",filename);
  72.     retval = fopen(filename,mode);
  73.     if (retval == NULL)
  74.         {
  75.         printf("Can't open file \"%s\"\n",filename);
  76.         exit(1);
  77.         }
  78.     else
  79.         return retval;
  80.     }
  81.  
  82. /****************************************************************
  83. * main - get the files and keys, encrypt the text and quit        *
  84. ****************************************************************/
  85. int main(void)
  86.     {
  87.     int n_wheels;                    /* Number of keys in use    */
  88.     int c;                            /* The character to encrypt    */
  89.     int i;                            /* Loop variable            */
  90.  
  91.     FILE *inf, *outf;                /* Input & output file ptrs    */
  92.  
  93.     /* Open the input and output files */
  94.     inf  = GetFile("Input file name : ","rb");
  95.     outf = GetFile("Output file name: ","wb");
  96.  
  97.     /* Get the number of keys. */
  98.     printf("How many keys to use? ");
  99.     scanf("%d",&n_wheels);
  100.     if (n_wheels > MAX_WHEELS)
  101.         {
  102.         printf("The maximum number of keys is %d.\n",MAX_WHEELS);
  103.         return 1;
  104.         }
  105.  
  106.     /* Now get the keys. */
  107.     for (i = 0; i < n_wheels; i++)
  108.         {
  109.         printf("Enter key %d : ",i+1);
  110.         scanf("%32s",wheel[i].value);
  111.         if (!*wheel[i].value)        /* A null key terminates    */
  112.             {                        /* the list of keys            */
  113.             n_wheels = i+1;            /* Reset n_wheels            */
  114.             break;                    /* And get out of the loop. */
  115.             }
  116.         }
  117.  
  118.     /* Set the wheels to the first position. */
  119.     tdInit();
  120.  
  121.     /* Now cycle through plaintext encrypting each character    */
  122.     while( (c = getc(inf)) != EOF )
  123.         {
  124.         c = tdEncrypt(c);        /* Encrypt the character        */
  125.         putc( c, outf);            /* Write char to output file    */
  126.         }
  127.  
  128.     /* Neatness counts... */
  129.     fclose(inf);
  130.     fclose(outf);
  131.  
  132.     return 0;
  133.     }
  134.